package com.yylending.las.common.widgets;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.main.InviteCodeVerifyPresenter;

/**
 * Created by ying_he on 2017/9/5.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class InviteCodeVerifyDialog extends Dialog implements UnderLineEditText.OnVerificationCodeChangedListener{

	private TextView mInviteCodeTipsTv;
	private UnderLineEditText mInviteCodeEdt;

	private InviteCodeVerifyPresenter mInviteCodeVerifyPresenter;
	private String mInviteCode = "";

	public InviteCodeVerifyDialog(@NonNull Context context) {
		super(context, R.style.loading_progress_dialog);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LayoutInflater inflater = LayoutInflater.from(getContext());
		View view = inflater.inflate(R.layout.dialog_invite_vode_verify, null);
		mInviteCodeTipsTv = (TextView) view.findViewById(R.id.invite_code_tips_tv);
		mInviteCodeEdt = (UnderLineEditText) view.findViewById(R.id.invite_code_edt);
		setContentView(view);
		mInviteCodeEdt.setOnVerificationCodeChangedListener(this);
		setCanceledOnTouchOutside(false);
	}

	@Override
	public void onVerCodeChanged(CharSequence s, int start, int before, int count) {
		mInviteCodeTipsTv.setText(getContext().getString(R.string.should_input_invite_code));
		mInviteCodeTipsTv.setTextColor(getContext().getResources().getColor(R.color.color_7eb8fc));
	}

	@Override
	public void onInputCompleted(CharSequence s) {
		YJKD.hideInputWindow(this);
		mInviteCode = s.toString();
		if(null == mInviteCodeVerifyPresenter){
			dismiss();
		}else{
			mInviteCodeVerifyPresenter.submitInviteCode(mInviteCode);
		}
	}

	public void setInviteCodeVerifyPresenter(InviteCodeVerifyPresenter inviteCodeVerifyPresenter){
		mInviteCodeVerifyPresenter = inviteCodeVerifyPresenter;
	}

	public void clearInviteCodeEdt(){
		if (mInviteCodeEdt != null) {
			mInviteCodeEdt.setText("");
		}
	}

	public void setInviteCodeErrorTips(){
		mInviteCodeTipsTv.setText(getContext().getString(R.string.invite_code_error));
		mInviteCodeTipsTv.setTextColor(getContext().getResources().getColor(R.color.color_f74e28));
	}

	@Override
	public void show() {
		super.show();
		mInviteCodeEdt.requestFocus();
		YJKD.showInputWindow();
	}
}
